// pages/profile/info_check/index.js
const qiniuUploader = require("../../../utils/qiniuUploader")
import { get, post,put } from '../../../utils/request';
import {getUploadUrl,getDictData} from '../../../api/common';
import {getUserInfo,findCardInfo} from '../../../api/user';
import {uploadFile} from '../../../utils/upload';
import Cache from '../../../utils/cache.js';
import util from '../../../utils/util';
import {jobResume} from '../../../api/jobs';
import validate from '../../../utils/validate';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: -1,
    imgList: [ 
    ],
    educationList: ["不限","小学", "中专", "大专", "本科", "硕士", "博士"],
    selectedEducation: "不限",
    isBottomSheetVisible :  false,
    lg_degree : [],
    system_user_sex : [],
    lg_yes_no  : [],
    sex : 1,
    degree : '',
    selectedLabelDegree : '请选择学历',
    address : '',
    handicapped:0,
    id_card : '',
    name : '',
    soldier : 0,
    jobs : '点击选择',
    selectedWork:"",
    wages:'',
    introduce : ''
  },
  card_input(e){
    this.setData({
      id_card : e.detail.value
    })
  },
  name_input(e){
    this.setData({
      name : e.detail.value
    })
  },
  wages_input(e){
    this.setData({
      wages : e.detail.value
    })
  },
  introduce_input(e){
    this.setData({
      introduce : e.detail.value
    })
  },
  select_jobs(){
    wx.navigateTo({
      url: "/pages/job_types/job_types",
    })
  },
  changeSexRadio(e){
    this.setData({
      sex : e.detail.value
    })
  },
  changeHandicappedRadio(e){
    this.setData({
      handicapped : e.detail.value
    })
  },
  changeSoldierRadio(e){
    this.setData({
      soldier : e.detail.value
    })
  },
  onPickerChangeAddress(e){
    //地区选择器
    this.setData({
      address : e.detail.value[0]+e.detail.value[1]+e.detail.value[2]
    })
  },
  onPickerChangeDegree(e){
    console.log(e)
    //学历选择
    const index = e.detail.value; // 获取选中项的索引
    const selected = this.data.lg_degree[index]; // 根据索引找到对应的选项
    this.setData({
      degree: selected.value, // 保存选中的 value
      selectedLabelDegree: selected.label // 保存选中的 label
    });
  },
  show_edu(){
      this.setData({
        isBottomSheetVisible :true
      })
  },
  confirmBottomSheet(){
    this.setData({
      isBottomSheetVisible :false
    })
  },

  closeBottomSheet(){
    this.setData({
      isBottomSheetVisible :false
    })
  },
   //关闭弹窗
   closePopup() {
    this.setData({
      isBottomSheetVisible: false,
    });
  },
  selectSheetOption(e){
    const value = e.currentTarget.dataset.value;
    this.setData({
      selectedEducation :value
    })
  },
  uploadMultipleFiles(filePaths) {
    // 使用 Promise.all 并发上传
    const uploadTasks = filePaths.map((filePath) => uploadFile(filePath));
    return Promise.all(uploadTasks);
  },
  ChooseImageIdCard(){
    var that = this
    //身份证识别
    wx.chooseMedia({
      count:1,
      mediaType:['image'],
      success(res){
        var file_path = res.tempFiles[0].tempFilePath
        uploadFile(file_path).then(file_url=>{
           findCardInfo(file_url).then(res=>{
             util.Tips('识别成功')
            that.setData({
                  id_card : res.data.id,
                  name  : res.data.name,
                  gender : res.data.gender == '男' ? 1 : 2
               })
            })
        })
       
      }
    })
  },
  ChooseImage() {
    wx.chooseImage({
        count: 3, 
        sizeType: ['original', 'compressed'], // 原图或压缩图
        sourceType: ['album', 'camera'], // 从相册或拍照获取
        success: (res) => {
          const tempFilePaths = res.tempFilePaths; // 获取选中的图片路径数组
  
          // 调用批量上传方法
          this.uploadMultipleFiles(tempFilePaths)
            .then((uploadedPaths) => {
       
              // 更新图片列表
              this.setData({
                imgList: this.data.imgList.concat(uploadedPaths),
              });
            })
            .catch((error) => {
              console.error('上传失败：', error);
            });
        },
        fail: (err) => {
          console.error('选择图片失败：', err);
        },
      });
  },
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    wx.showModal({
      title: '删除',
      content: '确定要删除吗？',
      cancelText: '再看看',
      confirmText: '再见',
      success: res => {
        if (res.confirm) {
          this.imgList.splice(e.currentTarget.dataset.index, 1)
        }
      }
    })
  },
  save_user_info(){
   
    if(validate.isEmpty(this.data.address) ){
      return util.Tips('请选择区域')
    }
    if(validate.isEmpty(this.data.id_card) ){
      return util.Tips('请填写身份证')
    }
    if(validate.isEmpty(this.data.sex) ){
      return util.Tips('请选择性别')
    }
    if(validate.isEmpty(this.data.selectedWork) ){
      return util.Tips('请选择期望工种')
    }

    if(validate.isEmpty(this.data.degree) ){
      return util.Tips('请选择学历')
    }
    if(validate.isEmpty(this.data.wages) ){
      return util.Tips('请选择期望工资')
    }
    if(validate.isEmpty(this.data.introduce) ){
      return util.Tips('请填写自我介绍')
    }
    jobResume({
      name : this.data.name,
      card : this.data.id_card,
      sex :  this.data.sex,
      adress : this.data.address,
      degree : this.data.degree,
      handicapped : this.data.handicapped,
      soldier : this.data.soldier,
      wages   : this.data.wages,
      introduce : this.data.introduce,
      jobs  : this.data.selectedWork,
      certificate : this.data.imgList.length ? this.data.imgList.join(',') : ''
    }).then(res=>{
        util.Tips('操作成功');
        setTimeout(function(){
          wx.navigateBack()
        },500)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.get_user_info();
    this.init();
  },
  init(){
    getDictData('system_user_sex').then(res=>{
       this.setData({
          system_user_sex:res.data
       })
    })
    getDictData('lg_yes_no').then(res=>{
        this.setData({
          lg_yes_no:res.data
        })
    })
    getDictData('lg_degree').then(res=>{
      this.setData({
        lg_degree:res.data
      })
      
  })
  },
  get_user_info(){
    getUserInfo().then(res=>{

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
     var selectedWork =  Cache.get('selectedWork')
     var job          = Cache.get('job')
     if(selectedWork && job){
       this.setData(
         {jobs : job,selectedWork:selectedWork}
       )
     }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})